<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <title>设置我的密码</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">修改密码</div>
          <div class="layui-card-body" pad15>
            
            <div class="layui-form" lay-filter="">
              <div class="layui-form-item">
                <label class="layui-form-label">当前密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="oldPassword" lay-verify="required" lay-verType="tips" class="layui-input"/>
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="password" lay-verify="pass" lay-verType="tips" autocomplete="off" id="LAY_password" class="layui-input"/>
                </div>
                <div class="layui-form-mid layui-word-aux">6到16个字符</div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">确认新密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="confirmPassword" lay-verify="repass" lay-verType="tips" autocomplete="off" class="layui-input"/>
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-input-block">
                  <input name="id" type="hidden" th:value="${user.id}" />
                  <input name="userName" type="hidden" th:value="${user.userName}" />
                  <button class="layui-btn" lay-submit lay-filter="updatePassword">确认修改</button>
                </div>
              </div>
            </div>
            
          </div>
        </div>
      </div>
    </div>
  </div>

  <script th:src="@{/statics/layui/layui.js}"></script>
  <script>
  layui.config({
    base: '/statics/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','form'],function (t) {
      var $ = layui.$
          ,admin = layui.admin
          ,form = layui.form;

      form.render();

      form.verify({
          pass: [/^[\S]{6,12}$/, "密码必须6到12位，且不能出现空格"],
          repass: function (t) {
              if (t !== $("#LAY_password").val()) return "两次密码输入不一致"
          }
      });

      form.on("submit(updatePassword)", function (data) {
          console.info(data.field);
          //请求登入接口
          $.ajax({
              url: '/api/user/changePassword',
              method: 'post',
              data: data.field,
              dataType:'JSON',
              async: true,
              success: function(res){
                  //登入成功的提示与跳转
                  if(res.code == 0){
                      layer.msg('修改密码成功，请重新登录！', {
                          offset: '15px'
                          ,icon: 1
                          ,time: 2000
                      }, function(){
                          top.location.href = '/logout'; //后台主页
                      });
                  }else{
                      layer.msg(res.msg,{icon: 2,time:3000});
                  }
              },
              error:function (res) {
                  layer.msg(res.data, {icon: 5});
              }
          });
      });
  });
  </script>
</body>
</html>